<?php
// 应用公共文件

if (!function_exists('result')) {
    /**
     * 获取当前json对象实例.
     *
     * @return object
     */
    function result()
    {
        return app('result');
    }
}
/**
 * 根据path转成id
 * @param string $path
 * @return number
 */
function ossPathToId($path) {
    $path = explode('?', $path);
    $path = explode(config('oss.endpoint').'/', $path[0]);
    $info = \think\facade\Db::table('car_file_oss')->where('delPath', $path[1])->findOrEmpty();
    return $info['id'];
}
function rand_number($length)
{
    return rand(pow(10, ($length - 1)), pow(10, $length) - 1);
}
function ossContentType($value)
{
    $arr = [
        'jpg' => 'image/jpeg',
        'gif' => 'image/gif',
        'png' => 'image/png',
        'webp' => 'image/webp',
        'zip' => 'application/zip',
        'tar' => 'application/x-tar',
    ];
    if(isset($arr[$value])) {
        return $arr[$value];
    } else {
        return  'application/octet-stream';
    }
}
function getRandomIP()
{
    return mt_rand(1, 255) . '.' . mt_rand(1, 255) . '.' . mt_rand(1, 255) . '.' . mt_rand(1, 255);
}

function getImageHeight($image_url, $newWidth=750)
{
    $contextOptions = [
        "ssl" => [
            "verify_peer" => false,
            "verify_peer_name" => false,
        ],
    ];
    $context = stream_context_create($contextOptions);
    $imageData = file_get_contents($image_url, false, $context);
    $sizeInfo = getimagesizefromstring($imageData);
    $width = $sizeInfo[0];
    $height = $sizeInfo[1];
    $newHeight = $height * $newWidth / $width;
    $newHeight = sprintf("%.2f", $newHeight);
    return $newHeight;
}

/**
 * 生成指定位数随机字母
 * @param string $length
 * @return string
 */
function rand_letter($length) {
    $str = null;
    $strPol = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
    $max = strlen($strPol) - 1;
    for ($i = 0; $i < $length; $i++) {
        $str .= $strPol[rand(0, $max)];
    }
    return $str;
}

function make_coupon_card() {
    $code = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $rand = $code[rand(0,25)]
        .strtoupper(dechex(date('m')))
        .date('d').substr(time(),-5)
        .substr(microtime(),2,5)
        .sprintf('%02d',rand(0,99));
    for(
        $a = md5( $rand, true ),
        $s = '0123456789ABCDEFGHIJKLMNOPQRSTUV',
        $d = '',
        $f = 0;
        $f < 8;
        $g = ord( $a[ $f ] ),
        $d .= $s[ ( $g ^ ord( $a[ $f + 8 ] ) ) - $g & 0x1F ],
        $f++
    );
    return  $d;
}

function sign_day_integral($day)
{
    if($day<= 7 ) {
        return $day;
    }
    $randomNumber = rand(8, 20);
    return $randomNumber;
}

function backNewAndDelTags($oldTags, $newTags)
{
    $addTags = [];
    $delTags = [];
    foreach ($newTags as $key => $item) {
        if(!in_array($item, $oldTags)) {
            $addTags[] = $item;
        }
    }
    foreach ($oldTags as $key => $item) {
        if(!in_array($item, $newTags)) {
            $delTags[] = $item;
        }
    }
    return [
        'add' => $addTags,
        'del' => $delTags
    ];
}

function isToday($timestamp) {
    $today = strtotime(date('Y-m-d'));
    return date('Y-m-d', $timestamp) == date('Y-m-d', $today);
}

/**
 * Reverse a multibyte string
 * @param string $str
 * @param string $encoding
 * @return string
 */
function mb_strrev($str, $encoding = 'UTF-8') {
    $length = mb_strlen($str, $encoding);
    $reversedStr = '';
    while ($length-- > 0) {
        $reversedStr .= mb_substr($str, $length, 1, $encoding);
    }
    return $reversedStr;
}

function signUrl($path, $fun = 'tt')
{
    $path = explode('.', $path);
    $path = $path[0];
    $now = time();
    $str = rand_letter(7);
    $newStr = $path.$now.$str;
    $newStr = mb_strrev($newStr);
    $newStr = md5($newStr);
    $newStr = str_split($newStr);
    sort($newStr);
    $newStr = implode('', $newStr);
    $newStr = md5($newStr);
    $newStr = str_split($newStr);
    $newStr = array_filter($newStr, function($key) {
        return !in_array($key, [2, 6, 8, 10]);
    }, ARRAY_FILTER_USE_KEY);
    $newStr = implode('', $newStr);
    $newStr = md5($newStr);
    return '/image/'.$fun.'?path='.$path.'&time='.$now.'&sign='.$newStr.'&str='.$str;
}

function signUrlTwo($path, $fun = 'ttTwo')
{
    $now = time();
    $str = rand_letter(7);
    $newStr = $path.$now.$str;
    $newStr = mb_strrev($newStr);
    $newStr = md5($newStr);
    $newStr = str_split($newStr);
    sort($newStr);
    $newStr = implode('', $newStr);
    $newStr = md5($newStr);
    $newStr = str_split($newStr);
    $newStr = array_filter($newStr, function($key) {
        return !in_array($key, [2, 6, 8, 10]);
    }, ARRAY_FILTER_USE_KEY);
    $newStr = implode('', $newStr);
    $newStr = md5($newStr);
    return '/image/'.$fun.'?path='.$path.'&time='.$now.'&sign='.$newStr.'&str='.$str;
}